<template>
    <div class="product-list-container">
        <!-- 商品列表 -->
        <div class="product-list">
            <!-- 商品项 - 循环渲染 -->
            <div class="" v-for="(product, index) in products" :key="index">
                <div class="product-item">
                    <div style=" display: flex;align-items: center;" @click="edit(product)">
                        <!-- 商品图片 -->
                        <div class="product-image">
                            <img :src="$Img + product.img" alt="" class="product-img">
                        </div>

                        <!-- 商品信息 -->
                        <div class="product-info">
                            <h3 class="product-name">{{ product.name }}</h3>
                            <p class="product-price">¥{{ product.price.toFixed(2) }}</p>
                            <p v-if="product.isPromotion == '1'" class="product-name">促销</p>
                        </div>

                        <!-- 操作按钮 -->
                        <div class="product-actions">
                            <button class="action-btn" :class="{ '下架': product.state == 1, '上架': product.state != 1 }" 
                                @click.stop="toggleStatus(product)">
                                {{ product.state == 1 ? '下架' : '上架' }}
                            </button>
                        </div>
                    </div>
                </div>


            </div>
        </div>
        <div class="addBtn" @click="jump">
            新增
        </div>
        <div class="addBtns" @click="jumpOne">
            订单中心
        </div>
    </div>
</template>
  
<script>
import { storeList,pullStore} from "@/api/fulfil.js";
export default {
    name: 'storeList',
    data() {
        return {
            // 商品列表数据
            products: [
            ]
        };
    },
    activated() {
        this.getIndex();
    },
    methods: {
        //获取列表数据
        getIndex() {
            let qyery = {
                agentid:localStorage.getItem("agentid")
            }
            storeList(qyery)
                .then((res) => {
                    console.log(res, 'res');
                        this.products = res.data.data;
                    
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        //修改
        edit(product){
            this.$router.push({
                path: "/storeIndex",
                query: {
                    status: 2,
                    product:JSON.stringify(product)
                }
            });
        },
        //新增
        jump() {
            this.$router.push({
                path: "/storeIndex",
                query: {
                    status: 1
                }
            });
        },
        //订单中心
        jumpOne(){
            this.$router.push({
                path: "/order",
                query: {
                    status: 1
                }
            });  
        },
        // 切换商品上下架状态
        toggleStatus(data) {
            let query = {
                agentid:localStorage.getItem("agentid"),
                id: data.id, //商品id
                pull: data.state == 1 ? '0' : '1', //0 下架 1 上架
            }
            pullStore(query)
                .then((res) => {
                    if (res.data.code == 0) { 
                        this.$toast("操作成功");
                        this.getIndex();
                    } else {
                        this.$toast(res.data.msg);
                    }

                })
                .catch((error) => {
                    console.log(error);
                }); 
        },

        // 显示提示信息
        showToast(message) {
            // 简单的提示实现，实际项目中可使用UI库的toast组件
            const toast = document.createElement('div');
            toast.className = 'toast';
            toast.textContent = message;
            document.body.appendChild(toast);

            // 3秒后移除提示
            setTimeout(() => {
                toast.classList.add('fade-out');
                setTimeout(() => {
                    document.body.removeChild(toast);
                }, 300);
            }, 3000);
        }
    }
};
</script>
  
<style scoped>
.product-list-container {
    max-width: 100%;
    margin: 0 auto;
    padding: .375rem .375rem 1.5rem;
    background-color: #f5f5f5;
    min-height: 100vh;
    box-sizing: border-box;
}

/* 页面标题 */
.page-header {
    padding: .375rem 0;
    border-bottom: 1px solid #e5e5e5;
    margin-bottom: .5rem;
}

.page-header h1 {
    margin: 0;
    font-size: .5rem;
    color: #333;
    text-align: center;
}

/* 商品列表 */
.product-list {
    display: flex;
    flex-direction: column;
    gap: .25rem;
}

/* 商品项 */
.product-item {

    background-color: #fff;
    border-radius: .2rem;
    padding: .3275rem;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    transition: transform 0.2s;
}

.product-item:hover {
    transform: translateY(-2px);
}

/* 商品图片 */
.product-image {
    width: 2rem;
    height: 2rem;
    margin-right: .375rem;
    flex-shrink: 0;
}

.product-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: .1rem;
}

/* 商品信息 */
.product-info {
    flex: 1;
}

.product-name {
    margin: 0 0 .125rem 0;
    font-size: .4rem;
    color: #333;
    font-weight: 500;
}

.product-price {
    margin: 0;
    font-size: .45rem;
    color: #ff4d4f;
    font-weight: bold;
}

/* 操作按钮 */
.product-actions {
    margin-left: .375rem;
}

.action-btn {
    padding: .2rem .4rem;
    border-radius: .05rem;
    font-size: .35rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    border: none;
}

/* 上架/下架按钮样式区分 */
.action-btn.上架 {
    background-color: #52c41a;
    color: #fff;
}

.action-btn.上架:hover {
    background-color: #43a047;
}

.action-btn.下架 {
    background-color: #faad14;
    color: #fff;
}

.action-btn.下架:hover {
    background-color: #f59f00;
}

/* 提示框样式 */
.toast {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: .25rem .5rem;
    background-color: rgba(0, 0, 0, 0.7);
    color: white;
    border-radius: .1rem;
    font-size: .35rem;
    z-index: 1000;
    transition: opacity 0.3s;
}

.toast.fade-out {
    opacity: 0;
}

.addBtn {
    position: fixed;
    bottom: .1rem;
    left: 10%;
    width: 38%;
    text-align: center;
    height: 1rem;
    line-height: 1rem;
    font-size: 0.4rem;
    color: #fff;
    border-radius: 0.5rem;
    background-color: #52c41a;
    font-weight: 600;
}

.addBtns {
    position: fixed;
    bottom: .1rem;
    right: 10%;
    width: 38%;
    text-align: center;
    height: 1rem;
    line-height: 1rem;
    font-size: 0.4rem;
    color: #fff;
    border-radius: 0.5rem;
    background-color: #faad14;
    font-weight: 600;
}
</style>